package persistance.dal;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import ucc.objects.ContenanceDTO;
import exception.DataAccesException;

public class ContenancePersisterImpl implements ContenancePersister {

	private PreparedStatement queryInsert;
	private PreparedStatement queryUpdate;
	private static final String INSERT = "insert into caisse.contenances (quantite, unite)"
			+ "values (?, ?)";
	private static final String UPDATE = "update caisse.contenances set "
			+ "quantite=?, unite=? where id_contenance=?";

	public ContenancePersisterImpl() {
		this.queryInsert = DalBackEndServices.INSTANCE.prepare(INSERT);
		this.queryUpdate = DalBackEndServices.INSTANCE.prepare(UPDATE);
	}

	public void insert(ContenanceDTO nouvelleContenance) {
		try {
			queryInsert.setDouble(1, nouvelleContenance.getQuantite());
			queryInsert.setString(2, nouvelleContenance.getUnite());
			queryInsert.execute();
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();
			throw new DataAccesException(
					"SQL/JDBC Connection, Statement, executeQuery problem \n",
					sqlE);
		}
	}

	public void update(ContenanceDTO contenance) {
		try {
			queryInsert.setDouble(1, contenance.getQuantite());
			queryInsert.setString(2, contenance.getUnite());
			queryUpdate.setInt(2, contenance.getId());
			queryUpdate.execute();
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();
			throw new DataAccesException(
					"SQL/JDBC Connection, Statement, executeQuery problem \n",
					sqlE);
		}
	}
}
